package com.xiaxueqi.service.impl;

import org.springframework.beans.factory.annotation.Autowired;

import com.niagara.dao.BaseDao;
import com.xiaxueqi.entity.security.AppRole;
import com.xiaxueqi.entity.security.AppUser;
import com.xiaxueqi.entity.security.AppUserDetail;
import com.xiaxueqi.entity.security.AppUserRole;
import com.xiaxueqi.service.RegService;
import com.xiaxueqi.service.ServiceException;

public class RegServiceImpl implements RegService {
	@Autowired
	private BaseDao baseDao;

	public void setBaseDao(BaseDao baseDao) {
		this.baseDao = baseDao;
	}

	public void doRegister(AppUser au, AppUserDetail aud, AppUserRole aur)
			throws ServiceException {
		if (au == null)
			throw new ServiceException();
		this.baseDao.insert(au);
		if (aud == null)
			throw new ServiceException();
		aud.setId(au.getId());
		this.baseDao.insert(aud);
		AppRole arc = new AppRole();
		arc.setName(AppRole.ROLE_ADMIN);
		AppRole ar = this.baseDao
				.get(AppRole.class, arc, "select_by_condition");
		if (ar == null)
			throw new ServiceException();
		if (aur == null)
			throw new ServiceException();
		aur.setUserId(au.getId());
		aur.setRoleId(ar.getId());
		this.baseDao.insert(aur);
	}

	public void doActive(AppUser au, AppUserDetail aud, AppUserRole aur)
			throws ServiceException {
		// TODO Auto-generated method stub
		if (au == null || aud == null || aur == null)
			throw new ServiceException();
		baseDao.update(aud);
		baseDao.update(au);
		baseDao.update(aur);
	}

}
